package com.ws.base.db.base;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;

import com.ws.base.common.util.WSUtil;
import com.ws.base.db.common.page.Page;

/**
 * DBBaseOperate
 * 
 * @author wangshi
 */
public class DBBaseOperate {
    public boolean isPrintSqlStr = false;

    public int add(String sql, Map<String, Object> condition) {
        int rs = namedParameterJdbcOperations.update(sql, condition);
        return rs;
    }

    public int[] addBatch(String sql, Map<String, Object>[] conditions) {
        int[] rs = namedParameterJdbcOperations.batchUpdate(sql, conditions);
        return rs;
    }

    public int delete(String sql, Map<String, Object> condition) {
        int rs = namedParameterJdbcOperations.update(sql, condition);
        return rs;
    }

    public int update(String sql, Map<String, Object> condition) {
        int rs = namedParameterJdbcOperations.update(sql, condition);
        return rs;
    }

    public List<Map<String, Object>> query(String sql,
            Map<String, Object> condition, Page page) {
        if (null != page) {
            int rsRows = count(sql, condition);
            page.setTotalRows(rsRows);
            sql = page.setPageStr(sql);
        }

        this.printSqlStr(sql, condition);

        List<Map<String, Object>> list = namedParameterJdbcOperations
                .queryForList(sql, condition);
        WSUtil.MapKeytoLowerCase(list);
        return list;
    }

    public <T> List<T> queryListForObject(String sql,
            Map<String, Object> condition, Page page, Class<T> clazz) {
        if (null != page) {
            int rsRows = count(sql, condition);
            page.setTotalRows(rsRows);
            sql = page.setPageStr(sql);
        }
        List<T> list = namedParameterJdbcOperations.queryForList(sql,
                condition, clazz);
        return list;
    }

    public int count(String sql, Map<String, Object> condition) {
        sql = "SELECT COUNT(1) AS countNum FROM (" + sql + ") T_TEMP_E";

        return namedParameterJdbcOperations.queryForInt(sql, condition);
    }

    private NamedParameterJdbcOperations namedParameterJdbcOperations;

    public NamedParameterJdbcOperations getNamedParameterJdbcOperations() {
        return namedParameterJdbcOperations;
    }

    public void setNamedParameterJdbcOperations(
            NamedParameterJdbcOperations namedParameterJdbcOperations) {
        this.namedParameterJdbcOperations = namedParameterJdbcOperations;
    }

    private final void printSqlStr(String sql, Map<String, Object> condition) {
        if (isPrintSqlStr) {
            System.out.println(sql);

            for (Entry<String, Object> e : condition.entrySet()) {
                System.out.println(e.getKey() + " : " + e.getValue());
            }
        }
    }
}
